<?php
namespace app\controller;

use app\BaseController;
use app\Model\SurveysHistroryModel;
use app\Model\SurveysModel;
use app\Services\MioServices;
use app\Services\PvpServices;
use think\Response;

class Index extends BaseController
{
    public function index(SurveysModel $model)
    {
        return Response::create(['code'=>200,'data'=>$model->select()->toArray()], 'json');
    }

    public function copyUrl(){
        $info = (new SurveysModel())->where('id',$_GET['three_id'])->find()->toArray();
        if($info['type'] == 'pvp'){
            $hash_str = '%%PID%%';
            $hash = uniqid();
            $url_link = urldecode($info['links']);
            $url = str_replace($hash_str,$hash,$url_link);
        }else if($info['type'] == 'mio'){
            //换取links
            if($info['links'] == ''){
                $url_link = MioServices::url_get($info['survey_id']);
                (new SurveysModel())->where('id',$_GET['three_id'])->save([
                   'links'=>$url_link
                ]);
                $info['links'] = $url_link;
            }
            $hash_str = 'xxxxx';
            $hash = uniqid().'ac';
            $url_link = urldecode($info['links']);
            $url = str_replace($hash_str,$hash,$url_link);
        }
        //新增
        (new SurveysHistroryModel())->insert([
            'hash'=>$hash,
            'three_id'=>$_GET['three_id'],
            'created_at'=>time(),
        ]);
        header('Location:'.$url);
    }


    public function quato(){
        $info = (new SurveysModel())->where('id',$_GET['three_id'])->find()->toArray();
        $data = MioServices::renshe($info['survey_id']);
        var_dump($data);
    }

    public function surveys($name = 'ThinkPHP6')
    {
        $list = (new SurveysModel())->select()->toArray();

        $insert = [];
        $surveyd_id = array_column($list,'survey_id');

        $datas = MioServices::surveys();
        foreach ($datas as $value){
            if(in_array($value['surveyNumber'],$surveyd_id)){
                continue;
            }
            $insert[] = [
                'survey_id'=>$value['surveyNumber'],
                'loi'=>$value['loi'],
                'ir'=>$value['ir'],
                'country'=>$value['countryCode'],
                'cpi'=>$value['cpi'],
                'created_at'=>time(),
                'updated_at'=>time(),
            ];
            if(count($insert) >= 100){
                break;
            }
        }
        (new SurveysModel())->insertAll($insert);

        return;

        $lists = PvpServices::surveys();

        $surveyd_id = array_column($list,'survey_id');

        foreach ($lists as $value){
            if(in_array($value['SurveyID'],$surveyd_id)){
                continue;
            }
            $insert[] = [
                'survey_id'=>$value['SurveyID'],
                'loi'=>$value['LOI'],
                'ir'=>$value['IR'],
                'country'=>$value['Country'],
                'cpi'=>$value['CPI'],
                'links'=>$value['SurveyRedirect'],
                'created_at'=>time(),
                'updated_at'=>time(),
                'desc'=>json_encode($value['QuestionsAndAnswers'],JSON_UNESCAPED_UNICODE),
            ];
            if(count($insert) >= 100){
                break;
            }
        }


        (new SurveysModel())->insertAll($insert);

    }



}
